package cn.anc.aonicardv.module.ui.recorder;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.OnClick;
import cn.anc.aonicardv.Constant;
import cn.anc.aonicardv.R;
import cn.anc.aonicardv.R2;
import cn.anc.aonicardv.event.ScreenDisplayEvent;
import cn.anc.aonicardv.module.adpter.recorder.SettingSelectAdapter;
import cn.anc.aonicardv.module.ui.MainActivity;
import cn.anc.aonicardv.module.ui.base.BaseActivity;
import cn.anc.aonicardv.util.DialogUtils;
import cn.anc.aonicardv.widget.WarningDialog;
import cn.anc.httpcontrolutil.CarControl;
import cn.anc.httpcontrolutil.CarControlSettings;

/**
 * Created by yangdai on 2017/6/20.
 */

public class SettingSelectActivity extends BaseActivity implements CarControl.OnHeartBeatCallBack, View.OnClickListener {

    @BindView(R2.id.tv_title)
    TextView titleTv;
    @BindView(R2.id.rv_setting_select)
    RecyclerView settingSelectRv;
    private SettingSelectAdapter settingSelectAdapter;
    private boolean isSingle;
    private CarControlSettings.SettingItem settingItem;
    public Dialog progressDialog;
    private WarningDialog warningDialog;
    private DialogUtils dialogUtils;
    private String key;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        setContentView(R.layout.activity_setting_select);
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    public void initVariable() {
        dialogUtils = new DialogUtils();
        progressDialog = dialogUtils.getCustomProgressDialog(this);
        warningDialog = dialogUtils.getWarningDialog(this);
    }

    @Override
    public void setListener() {
        warningDialog.okTv.setOnClickListener(this);
    }

    @OnClick(R2.id.tv_back)
    public void back() {
        finish();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(ScreenDisplayEvent event) {
        settingItem = CarControlSettings.getSettingItem(key);
        setAdapter();
    }

    private void setAdapter() {
        settingSelectAdapter = new SettingSelectAdapter(settingItem, isSingle, this);
        settingSelectRv.setAdapter(settingSelectAdapter);
    }

    @Override
    public void initData() {
        key = getIntent().getStringExtra(Constant.IntentKeyParam.SETTING_KEY);
        isSingle = getIntent().getBooleanExtra(Constant.IntentKeyParam.SELECT_MODE, true);
        settingItem = CarControlSettings.getSettingItem(key);
        settingSelectRv.setLayoutManager(new LinearLayoutManager(this));
        titleTv.setText(CarControlSettings.getSettingItemGuiResource(settingItem.settingItemKey).settingItemDisplayStrId);
        setAdapter();
    }

    public void settingMultipleValue() {
        progressDialog.show();
        String value = "";
        for (int i = 0; i < settingItem.settingItemOptions.size(); i++) {
            SettingSelectAdapter.ViewHolder v = (SettingSelectAdapter.ViewHolder) settingSelectRv.findViewHolderForAdapterPosition(i);
            if (v.switchButton.isChecked()) {
                if (TextUtils.isEmpty(value))
                    value = settingItem.settingItemOptions.get(i);
                else
                    value = value + "," + settingItem.settingItemOptions.get(i);
            }
        }
        settingItem.settingItemValue = value;
        CarControl.doSetSettingItemValue(settingItem.settingItemKey, settingItem.settingItemValue, new CarControl.OnCarControlCallback() {
            @Override
            public void OnCommandCallback(int code, CarControl.CommandResponseInfo info) {
                progressDialog.dismiss();
                finish();
            }
        });
    }
    @Override
    public void onResume() {
        super.onResume();
        CarControl.setOnHeartBeatCallBack(this);
        if (!CarControl.IsConnected.get()){
            if (null != warningDialog) {
                if (warningDialog.isShowing()) {
                    warningDialog.dismiss();
                }
                if (!warningDialog.isShowing() && !isDestroyed()) {
                    warningDialog.setHint(getString(R.string.tip_cardv_disconnect)).show();
                }

            }
        }

    }
    @Override
    public void OnHeartBeatConnectOK() {

    }

    @Override
    public void OnHeartBeatConnectFail() {

    }

    @Override
    public void OnHeartBeatDisconnect() {
        if (null != warningDialog)
            warningDialog.setHint(getString(R.string.tip_cardv_disconnect)).show();

    }

    @Override
    public void onClick(View v) {
        warningDialog.dismiss();
        startActivity(new Intent(this, MainActivity.class));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

}
